package com.ls.lscrm.web.service.impl;

import com.ls.lscrm.web.dao.CourseOrderDao;
import com.ls.lscrm.web.model.CRUDResult;
import com.ls.lscrm.web.model.CourseOrder;
import com.ls.lscrm.web.model.MonthIncome;
import com.ls.lscrm.web.model.PageResult;
import com.ls.lscrm.web.service.ICourseOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseOrderServiceImpl implements ICourseOrderService {

    @Resource
    CourseOrderDao dao;

    @Override
    public PageResult<CourseOrder> find(CourseOrder tj, int page, int pageSize) {
        PageResult<CourseOrder> result = new PageResult<CourseOrder>();
        Map<String, Object> map = new HashMap<>();
        map.put("start", (page - 1) * pageSize);
        map.put("limit", pageSize);
        map.put("courseorder", tj);
        //获取查询的数据
        List list = dao.findListByMap(map);
        //获取总记录数据
        int count = dao.findCountByMap(map);
        result.setCode(0);
        result.setMsg("");
        result.setCount(count);
        result.setData(list);
        return result;
    }

    @Override
    public CRUDResult save(CourseOrder order) {
        CRUDResult result = new CRUDResult();
        int i = dao.insert(order);
        if (i == 0) {
            result.setSuccess(0);
            result.setMsg("添加失败");
            return result;
        }
        result.setMsg("");
        result.setSuccess(1);
        return result;
    }

    @Override
    public CourseOrder findByOrderId(String order_id) {
        return dao.findByOrderId(order_id);
    }

    @Override
    public CRUDResult delete(String order_id) {
        CRUDResult result = new CRUDResult();
        int i = dao.deleteById(order_id);
        if (i == 0) {
            result.setSuccess(0);
            result.setMsg("删除失败");
            return result;
        }
        result.setMsg("");
        result.setSuccess(1);
        return result;
    }

    @Override
    public CRUDResult update(CourseOrder order) {
        CRUDResult result = new CRUDResult();
        int i = dao.update(order);
        if (i == 0) {
            result.setSuccess(0);
            result.setMsg("修改失败");
            return result;
        }
        result.setMsg("");
        result.setSuccess(1);
        return result;
    }

    @Override
    public List<MonthIncome> getMonthIncomes() {
        return dao.getMonthIncomes();
    }
}
